// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';
import 'cart/cart.dart';
import 'home/home.dart';
import 'mine/mine.dart';
import 'category/category.dart';
import '../utils/G.dart';

class index_page extends StatefulWidget {
  index_page({Key? key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<index_page> {
  void _changePage(int index) {
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
        // 4. 指定跳转的页面
      });
    }
  }

  // 底部导航菜单
  List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(icon: const Icon(Icons.home), label: '首页'),
    BottomNavigationBarItem(
        icon: const Icon(Icons.category_outlined), label: '分类'
        // label: G.t('category')
        ),
    BottomNavigationBarItem(icon: const Icon(Icons.shopping_cart), label: '购物车'
        // label: G.t('cart')
        ),
    BottomNavigationBarItem(icon: const Icon(Icons.person), label: '我的'
        // label: G.t('mine')
        ),
  ];

  // 四个页面
  List pages = [
    {
      "appBar": AppBar(
        // leading: null,
        title: Image.asset(
          'assets/images/logo.png',
          width: 100,
          height: 25,
        ),
        elevation: 0,
        // centerTitle: true,
        backgroundColor: Colors.white,
        actions: [
          InkWell(
              onTap: () {
                // print('跳转到商品搜素页面');
                G.router.navigateTo(G.getCurrentContext(), '/product_search');
              },
              child: Icon(Icons.search, size: 28, color: Colors.grey[800])),
          const SizedBox(
            width: 10,
          )
        ],
      ),
      "body": Home()
    },
    {
      "appBar":
          AppBar(title: const Text('商品分类'), elevation: 0, centerTitle: true),
      "body": Category()
    },
    {
      "appBar": null,

      // AppBar(
      //   title: Text('购物车'),
      //   elevation: 0,
      //   centerTitle: true
      // ),
      "body": Cart()
    },
    {
      "appBar":
          AppBar(title: const Text('我的'), elevation: 0, centerTitle: true),
      "body": Mine()
    },
  ];
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: pages[currentIndex]['appBar'],
        body: pages[currentIndex]['body'],
        bottomNavigationBar: BottomNavigationBar(
            items: bottomNavItems,
            currentIndex: currentIndex,
            type: BottomNavigationBarType.fixed,
            onTap: (index) {
              _changePage(index);
            }),
      ),
    );
  }
}
